//trait 关联类型使用,获取两值的差异及第一个值与最后一个值
struct Container(i32, i32);

// 使用关联类型实现特征
trait Contains {
    type A;
    type B;
    fn contains(&self, _: &Self::A, _: &Self::B) -> bool;
    fn first(&self) -> i32;
    fn last(&self) -> i32;
}

impl Contains for Container {
    // 关联类型具体化
    type A = i32;
    type B = i32;
    fn contains(&self, num_1: &i32, num_2: &i32) -> bool {
        (&self.0 == num_1) && (&self.1 == num_2)
    }
    fn first(&self) -> i32 { self.0}
    fn last(&self) -> i32 { self.1 }
}
//两值的差异
fn difference<T: Contains>(container: &T) -> i32 { container.last() - container.first()}
//fn difference(container: &impl Contains) -> i32 { container.last() - container.first()}

fn main() {
    let num_1 = 3; let num_2 = 10;
    let container = Container(num_1, num_2);
    println!("Does container contain {} and {}: {}", 
        &num_1, &num_2, container.contains(&num_1, &num_2));
    println!("First number: {}", container.first());
    println!("Last number: {}", container.last());
    println!("The difference is: {}", difference(&container));
}
